package com.devbobcorn.donutopia;

import java.awt.Color;

import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;

public class DonutColors {
    public static int getRainbowColorAt(BlockPos pos){
        Color foliage = Color.getHSBColor((((float)pos.getX() + MathHelper.sin(((float)pos.getZ() + (float)pos.getX()) / 35) * 35) % 150) / 150, 0.6F, 1.0F);
        return foliage.getRGB();
    }

	public static int getRainbowColorAt(int ypos){
        Color foliage = Color.getHSBColor(((float)(ypos % 16)) / 15.0F, 0.6F, 1.0F);
        return foliage.getRGB();
    }

	public static int getRainbowItemColor(ItemStack stack) {
		float frac = (float)stack.getCount() / stack.getMaxStackSize();
		return Color.getHSBColor(frac, 0.6F, 1.0F).getRGB();
	}
	
    public final static int black   = 0x000000;
    public final static int white   = 0xFFFFFF;
    public final static int pink1   = 0xB647FF;
	public final static int pink2   = 0xFF2AB0;
	public final static int pink3   = 0x6338C7;
	public final static int blue1   = 0x00D0FF;
	public final static int blue2   = 0x385DC6;
	public final static int blue3   = 0x567EEF;
	public final static int white1  = 0xFFE4F3;
	public final static int white2  = 0xFFFEFF;
	public final static int sweet1  = 0xFF3375;
	public final static int sweet2  = 0x8900BD;
	public final static int ignite1 = 0xFF7600;
	public final static int ignite2 = 0xFF6000;
    public final static int misty1 =  0x20E6FF;
	public final static int misty2 =  0xC6F1C3;

    public static final float loop = 8.0F;

    public static int getLoopColorAt(int ypos, int lc, int rc){
        double frac = MathHelper.sin(ypos * 3.14159265358976F / loop) / 2.0 + 0.5;
        return getRGBColorBetween(frac, lc, rc);
    }

    public static Color getLoopColorAt(int ypos, Color lc, Color rc){
        double frac = MathHelper.sin(ypos * 3.14159265358976F / loop) / 2.0 + 0.5;
        return getRGBColorBetween(frac, lc, rc);
    }

    public static int getBlockColorAt(int ypos,int min,int max,int minColor,int maxColor) {
		if (ypos >= max) return maxColor;
		else if (ypos <= min) return minColor;
		double frac = ((double)ypos - (double)min) / ((double)max - (double)min);
		
		return getRGBColorBetween(frac, minColor, maxColor);
	}
	
	public static int getItemColor(ItemStack stack,int lc,int rc) {
		double frac = (double)stack.getCount() / stack.getMaxStackSize();
		return getRGBColorBetween(frac,lc,rc);
	}
	
	public static int getRGBColorBetween(double frac,int lc,int rc) {
		int red1 = (lc & 0xff0000) >> 16;
    	int green1 = (lc & 0xff00) >> 8;
    	int blue1 = lc & 0xff;
    	
    	int red2 = (rc & 0xff0000) >> 16;
    	int green2 = (rc & 0xff00) >> 8;
    	int blue2 = rc & 0xff;
    	
    	int red3 = (int)MathHelper.lerp(frac,red1,red2);
    	int green3 = (int)MathHelper.lerp(frac,green1,green2);
    	int blue3 = (int)MathHelper.lerp(frac,blue1,blue2);
    	
    	return (red3 << 16) + (green3 << 8) + blue3;
	}

	public static Color getRGBColorBetween(double frac,Color lc,Color rc) {
    	return new Color((int)MathHelper.lerp(frac, lc.getRed(), rc.getRed()), (int)MathHelper.lerp(frac, lc.getGreen(), rc.getGreen()), (int)MathHelper.lerp(frac, lc.getBlue(), rc.getBlue()));
	}
	
	public static int getRed(int c) {
		return (c & 0xff0000) >> 16;
	}
	
	public static int getGreen(int c) {
		return (c & 0xff00) >> 8;
	}
	
	public static int getBlue(int c) {
		return (c & 0xff);
	}
	
	public static float getRedf(int c) {
		return (float)((c & 0xff0000) >> 16) / 255.0F;
	}
	
	public static float getGreenf(int c) {
		return (float)((c & 0xff00) >> 8) / 255.0F;
	}
	
	public static float getBluef(int c) {
		return (float)(c & 0xff) / 255.0F;
	}
}
